/*
 * Endless Code License
 * Copyright (C) 2009 Jingul, Kim
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ecGL.h"

JNIEXPORT void JNICALL Java_ec_gfx_GL_pointSize(JNIEnv *, jobject, jfloat size) {
	glPointSize(size);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_blendFunc(JNIEnv *, jobject, jint sfactor, jint dfactor) {
	glBlendFunc(sfactor, dfactor);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_pushAttrib(JNIEnv *, jobject, jint mask) {
	glPushAttrib(mask);
}


JNIEXPORT void JNICALL Java_ec_gfx_GL_pushMatrix(JNIEnv *env, jobject obj) {
	glPushMatrix();
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_popAttrib(JNIEnv *, jobject) {
	glPopAttrib();
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_popMatrix(JNIEnv *env, jobject obj) {
	glPopMatrix();
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_multiMatrix(JNIEnv *env, jobject obj,
		jfloatArray array) {
	jfloat *ar = env->GetFloatArrayElements(array, JNI_FALSE);
	glMultMatrixf((GLfloat*) ar);
	env->ReleaseFloatArrayElements(array, ar, JNI_ABORT);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_loadIdentity(JNIEnv *env, jobject obj) {
	glLoadIdentity();
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_matrixMode(JNIEnv *env, jobject obj,
		jint flag) {
	glMatrixMode(flag);
}

JNIEXPORT jint JNICALL Java_ec_gfx_GL_renderMode(JNIEnv *env, jobject obj,
		jint flag) {
	return glRenderMode(flag);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_clear(JNIEnv *env, jobject obj, jint flag) {
	glClear(flag);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_clearColor(JNIEnv *env, jobject obj,
		jfloat r, jfloat g, jfloat b, jfloat a) {
	glClearColor(r, g, b, a);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_translatef(JNIEnv *enb, jobject obj,
		jfloat x, jfloat y, jfloat z) {
	glTranslatef(x, y, z);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_scalef(JNIEnv *, jobject, jfloat x,
		jfloat y, jfloat z) {
	glScalef(x, y, z);
}


JNIEXPORT void JNICALL Java_ec_gfx_GL_rectf(JNIEnv *enb, jobject obj, jfloat x,
		jfloat y, jfloat w, jfloat h) {
	glRectf(x, y, w, h);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_initNames(JNIEnv *env, jobject obj) {
	glInitNames();
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_loadName(JNIEnv *env, jobject obj, jint i) {
	glLoadName(i);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_pushName(JNIEnv *env, jobject obj, jint i) {
	glPushName(i);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_popName(JNIEnv *env, jobject obj) {
	glPopName();
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_getFloatv(JNIEnv *env, jobject obj,
		jint flag, jfloatArray array) {
	jfloat *ar = env->GetFloatArrayElements(array, JNI_FALSE);
	glGetFloatv(flag, (GLfloat*) ar);
	env->ReleaseFloatArrayElements(array, ar, 0);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_getIntegerv(JNIEnv *env, jobject obj,
		jint flag, jintArray array) {
	jint *ar = env->GetIntArrayElements(array, JNI_FALSE);
	glGetIntegerv(flag, (GLint*) ar);
	env->ReleaseIntArrayElements(array, ar, 0);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_viewport(JNIEnv *env, jobject obj,
		jint x, jint y, jint w, jint h) {
	glViewport(x, y, w, h);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_ortho(JNIEnv *env, jobject obj,
		jdouble left, jdouble right, jdouble bottom, jdouble top, jdouble near,
		jdouble far) {
	glOrtho(left, right, bottom, top, near, far);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_color3f(JNIEnv *env, jobject obj,
		jfloat r, jfloat g, jfloat b) {
	glColor3f(r, g, b);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_selectBuffer(JNIEnv *env, jobject obj,
		jint size, jobject buffer) {
	GLuint *buf = (GLuint*) env->GetDirectBufferAddress(buffer);
	glSelectBuffer(size, buf);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_begin(JNIEnv *env, jobject obj, jint flag) {
	glBegin(flag);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_end(JNIEnv *env, jobject obj) {
	glEnd();
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_vertex2f(JNIEnv *env, jobject obj,
		jfloat x, jfloat y) {
	glVertex2f(x, y);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_vertex3f(JNIEnv *env, jobject obj,
		jfloat x, jfloat y, jfloat z) {
	glVertex3f(x, y, z);

}
JNIEXPORT void JNICALL Java_ec_gfx_GL_lineWidth(JNIEnv *env, jobject obj,
		jfloat w) {
	glLineWidth(w);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_rasterPos2i(JNIEnv *, jobject, jint x,
		jint y) {
	glRasterPos2i(x, y);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_drawPixels(JNIEnv *env, jobject, jint w,
		jint h, jint format, jint type, jobject obj) {
	void* buf = env->GetDirectBufferAddress(obj);
	glDrawPixels(w, h, format, type, buf);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_bitmap(JNIEnv *env, jobject, jint w,
		jint h, jint x, jint y, jint mx, jint my, jobject bitmap) {
	GLubyte* bit = (GLubyte*) env->GetDirectBufferAddress(bitmap);
	glBitmap(w, h, x, y, mx, my, bit);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_genTextures(JNIEnv *env, jobject, jint n,
		jobject obj) {
	GLuint* textures = (GLuint*)env->GetDirectBufferAddress(obj);
	glGenTextures(n, textures);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_deleteTextures(JNIEnv *env, jobject, jint n,
		jobject obj) {
	GLuint* textures = (GLuint*)env->GetDirectBufferAddress(obj);
	glDeleteTextures(n, textures);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_bindTexture(JNIEnv *, jobject,
		jint target, jint texture) {
	glBindTexture(target, texture);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_texParameteri(JNIEnv *, jobject, jint target,
		jint pname, jint param) {
	glTexParameteri(target, pname, param);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_texImage2D(JNIEnv *env, jobject,
		jint target, jint level, jint inFormat, jint w, jint h, jint border,
		jint format, jint type, jobject obj) {
	void* data = env->GetDirectBufferAddress(obj);
	glTexImage2D(target, level, inFormat, w, h, border, format, type, data);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_texCoord2f(JNIEnv *, jobject, jfloat s,
		jfloat t) {
	glTexCoord2f(s, t);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_enable(JNIEnv *, jobject, jint cap) {
	glEnable(cap);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_disable(JNIEnv *, jobject, jint cap) {
	glDisable(cap);
}

JNIEXPORT jint JNICALL Java_ec_gfx_GL_genLists(JNIEnv *, jobject, jint range) {
	return glGenLists(range);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_deleteLists(JNIEnv *, jobject, jint list, jint range) {
	glDeleteLists(list, range);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_newList(JNIEnv *, jobject, jint list, jint mode) {
	glNewList(list, mode);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_endList(JNIEnv *, jobject) {
	glEndList();
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_callList(JNIEnv *, jobject, jint list) {
	glCallList(list);
}

JNIEXPORT void JNICALL Java_ec_gfx_GL_callLists(JNIEnv *env, jobject, jint n, jint type, jobject obj) {
	GLvoid *lists = env->GetDirectBufferAddress(obj);
	glCallLists(n, type, lists);
}
